/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import pojo.SanBayPOJO;

/**
 *
 * @author b2
 */
public class SanBayDAO {

    public static ArrayList<SanBayPOJO> LayDanhSachSanBay() {
        ArrayList<SanBayPOJO> ds = new ArrayList<SanBayPOJO>();
        try {
            String sql = "SELECT * FROM SanBay";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                SanBayPOJO tb = new SanBayPOJO();
                tb.setMaSanBay(rs.getString("MaSanBay"));
                tb.setTenSanBay(rs.getString("TenSanBay"));
                ds.add(tb);
            }
            helper.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }

    public static SanBayPOJO laySanBay(String maSanBay) {
        SanBayPOJO tb = new SanBayPOJO();
        try {
            String sql = "SELECT * FROM SanBay WHERE MASANBAY = '" + maSanBay +"'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {

                tb.setMaSanBay(rs.getString("MaSanBay"));
                tb.setTenSanBay(rs.getString("TenSanBay"));
            }
            helper.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return tb;

    }

    // phuong th?c thêm 
    public static boolean themSanBay(SanBayPOJO sb) {
        boolean kq = false;
        String sql = String.format("INSERT INTO `SanBay` (`MaSanBay` ,`TenSanBay`)VALUES ('%s' , '%s');", sb.getMaSanBay(), sb.getTenSanBay());
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;
        }
        helper.close();

        return kq;
    }

    public static ArrayList<SanBayPOJO> TimKiemSanBayTheoTen(String tensanbay) {
        ArrayList<SanBayPOJO> ds = new ArrayList<SanBayPOJO>();
        try {
            String sql = "SELECT * FROM SANBAY WHERE TENSANBAY LIKE '%" + tensanbay + "%'";
            MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
            helper.open();
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()) {
                SanBayPOJO tb = new SanBayPOJO();
                tb.setMaSanBay(rs.getString("MaSanBay"));
                tb.setTenSanBay(rs.getString("TenSanBay"));

                //
                ds.add(tb);
            }
            helper.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return ds;
    }
    // phuong th?c xóa 

    public static boolean xoaSanBay(String maSanBay) {
        boolean kq = false;
        String sql = "DELETE FROM SanBay WHERE MaSanBay = '" + maSanBay + "'";
        MySqlDataAccessHelper helper = new MySqlDataAccessHelper();
        helper.open();
        int n = helper.executeUpdate(sql);
        if (n == 1) {
            kq = true;
        }
        helper.close();
        return kq;
    }
}
